<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

/**
 * 
 * 管理奖项控制
 * @author alang
 *
 */

class Reward extends Admin_controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->model('reward_mdl');
		$this->load->library('form_validation');
	}
	
	/**
	 * 奖项列表
	 * 
	 */
	public function index()
	{
		/*分页设置*/
		$enabled = intval($this->uri->rsegment(4));
		$page = $this->uri->rsegment(6);
	
		$page = ($page && is_numeric($page)) ? intval($page) : 1;
		$limit = 10;
		$offset = ($page - 1) * $limit;
		$pagination = '';
	
		$count = $this->reward_mdl->get_reward_count($enabled);
		$this->load->library('pagination');
		$config['base_url'] = base_url().'/adminroot/reward/index/enabled/'.$enabled.'/page/';
		$config['total_rows'] = $count;
		//设置url上第几段用于传递分页器的偏移量
		$config ['uri_segment'] = 7;	 
		$config['per_page'] = $limit; 
		$config['use_page_numbers'] = TRUE;
		$this->pagination->initialize($config); 
				
		$data = array();
		$data['list'] = $this->reward_mdl->get_reward_list(true,$limit, $offset,'DESC',$enabled);
		$data['page'] = $this->pagination->create_links();	
		$this->load->view('adminroot/list_reward',$data);
	}
	/**
	 * 编辑，更新奖项
	 */
	public function edit_reward()
	{
		$id = intval($this->uri->rsegment(4));
		$data['reward'] = $this->reward_mdl->get_reward_by_id($id);
		$this->_load_validation_rules();
		if($this->form_validation->run() == FALSE)
		{
			$this->load->view('adminroot/edit_reward',$data);
		}
		else 
		{
			//号码
			/*
			$rewardstr = $this->input->post('rewardnum');		
			if(!empty($rewardstr))
			{
				$rewardarr = array();
				$rewardarr = explode(',',$rewardstr);
				$rnum = count($rewardarr);
			}
			*/
			
			$maxnumber = intval($this->input->post('maxnumber'));
			$minnumber = intval($this->input->post('minnumber'));
			if($maxnumber < $minnumber){
				exit('自大数小于最小数');
			}
			for($i=$minnumber;$i<=$maxnumber;$i++){
				$rewardarr[] = $i;
			}
			
			$rewardstr = implode(',', $rewardarr);
			$rnum = count($rewardarr);
		
			//图片修改
			$updata = array();
			if(!empty($_FILES['rewardpic']['name'])){
				
				$config['upload_path'] = './upload/reward/';
				$config['allowed_types'] = 'gif|jpg|png';
				$config['encrypt_name'] = true;
				$config['overwrite'] = false;
			  	$this->load->library('upload', $config);
 
				  if ( ! $this->upload->do_upload('rewardpic'))
				  {
				   $error = array('error' => $this->upload->display_errors());
				  	var_dump($error);
				  }
				  $updata = $this->upload->data();
			}
			else {
				$updata['file_name'] = $this->input->post('rewardpicold');
			}
			echo $this->reward_mdl->update_reward(
				intval($this->input->post('id')),
				array(
					'infos' => $this->input->post('infos'),
					'prices' =>  $this->input->post('prices'),
					'products' => $this->input->post('products'),
					'rewardpic' => $updata['file_name'],
					'singleprice' => $this->input->post('singleprice'),
					'probability' => $this->input->post('probability'),
					'enabled' => $this->input->post('enabled'),
					'showtype' => $this->input->post('showtype'),
					'content' =>$this->input->post('content'),
					'rewardnum' =>$rewardstr,
					'rnum' => $rnum,
					'maxnumber' => $maxnumber,
					'minnumber' => $minnumber,
					'content' => $this->input->post('content'),
				)
			);
			
			//判断号码是否有修改
			$rewardnumold = $this->input->post('rewardnumold');
			if($rewardnumold !=$rewardstr)
			{
				$this->load->model('lottery_mdl','lottery');
				//删除掉原来的号码
				$this->lottery->del_rewardnum_by_rewardid(intval($this->input->post('id')));
				//添加号码到号码表 			
				$this->lottery->set_lottery_nums(intval($this->input->post('id')),$rewardarr);
			}

		}
		
	}
	
	/**
	 * 添加奖项
	 * 
	 */
	
	public function addreward()
	{
		
		$this->_load_validation_rules();
		if($this->form_validation->run() == FALSE)
		{
			$this->load->view('adminroot/add_reward');
		}
		else 
		{
			$updata = array();
			if(!empty($_FILES['rewardpic'])){
				
				$config['upload_path'] = './upload/reward/';
				$config['allowed_types'] = 'gif|jpg|png';
				$config['encrypt_name'] = true;
				$config['overwrite'] = false;
			  	$this->load->library('upload', $config);
 
				  if ( ! $this->upload->do_upload('rewardpic'))
				  {
				   $error = array('error' => $this->upload->display_errors());
				  	var_dump($error);
				  }
				  $updata = $this->upload->data();
			}
			$data['infos'] = $this->input->post('infos');
			$data['prices'] = $this->input->post('prices');
			$data['products'] = $this->input->post('products');
			$data['singleprice'] = $this->input->post('singleprice');
			$data['probability'] = $this->input->post('probability');
			$data['enabled'] = $this->input->post('enabled');
			$data['rewardpic'] = $updata['file_name'];
			$data['content'] = $this->input->post('content');
			$data['showtype'] = $this->input->post('showtype');	
			$data['maxnumber'] = $this->input->post('maxnumber');
			$data['minnumber'] = $this->input->post('minnumber');
			$rewardstr = $this->input->post('rewardnum');
			
			$rewardarr = array();
			if(!empty($rewardstr))
			{
				$data['rewardnum'] = $rewardstr;			
				$rewardarr = explode(',',$rewardstr);
				$data['rnum'] = count($rewardarr);
			}
			$result = $this->reward_mdl->add_reward($data);
			if($result)
			{
							
				//添加号码到号码表 
				$this->load->model('lottery_mdl','lottery');
				$this->lottery->set_lottery_nums($result,$rewardarr);
				
				
			}
			else 
			{
				echo 'error';
			}
		}
		
	}
	
	/**
	 * 删除奖项
	 * 
	 */
	public function del_reward()
	{
		$id = intval($this->uri->rsegment(4));
		$result = $this->reward_mdl->del_reward($id);
		redirect('adminroot/reward');
	}
	
	private function _load_validation_rules()
	{
		$this->form_validation->set_rules('infos','活动信息','required|xss_clean');
		$this->form_validation->set_rules('prices','中奖金额','required');
		$this->form_validation->set_rules('products','奖品','required');
		$this->form_validation->set_rules('singleprice','单位金额','required');
		$this->form_validation->set_rules('minnumber','最小可选数字');
		$this->form_validation->set_rules('maxnumber','最大可选数字');
		$this->form_validation->set_rules('probability','概率');
	}
	
}